/*
 * @Author       : NaN 353362604@qq.com
 * @Date         : 2023-01-12 09:35:49
 * @LastEditTime: 2023-04-23 18:34:35
 * @LastEditors: 飞越疯人院 353362604@qq.com
 * @FilePath     : \AdminStration\src\Pages\Front\Common\LargeScreen\TablePage.jsx
 * @Description  :
 */

import { useRef, useEffect, useState } from 'react'
import { ProTable } from '@ant-design/pro-components'
import request from 'umi-request'
import { TableMapping } from './TableMapping'

export const LargeScreeTable = ({ uuid, classification, className, ExperimentalThrEvent, testData, other }) => {
    const actionRef = useRef()
    const headers = { Authorization: sessionStorage.getItem('TOKEN') }
    const [TableConfigure, setTableConfigure] = useState()
    const [paramsData, setParamsData] = useState()
    useEffect(() => {
        const assemble = async () => {
            const data = await TableMapping[classification]({ uuid, className, ExperimentalThrEvent, other })
            setTableConfigure(data)
            actionRef.current?.reload()
        }
        assemble()
    }, [className, classification, uuid, ExperimentalThrEvent, other])
    return (
        <>
            {TableConfigure && (
                <ProTable
                    actionRef={actionRef}
                    className="TwoProTable"
                    columns={TableConfigure.columns}
                    cardBordered
                    params={TableConfigure?.params}
                    request={async (params = {}) => {
                        if (testData && testData.length !== 0) {
                            return { data: testData, success: true }
                        } else {
                            delete params.current
                            params.pageIndex = paramsData || 1
                            return request(TableConfigure.get, { params, headers }).then(data => {
                                if (data.list) return { data: data.list, total: data.total, success: true }
                                else return { data, success: true }
                            })
                        }
                    }}
                    editable={{ type: 'multiple' }}
                    columnsState={{
                        persistenceKey: 'pro-table-singe-demos',
                        persistenceType: 'localStorage',
                        onChange(value) {
                            // console.log('value: ', value)
                        },
                    }}
                    rowKey="uuid"
                    search={
                        TableConfigure.hideInSearch
                            ? false
                            : TableConfigure.abc
                            ? {
                                  labelWidth: 45,
                                  defaultCollapsed: false,
                                  span: 8,
                              }
                            : { labelWidth: 80 }
                    }
                    options={{ setting: { listsHeight: 400 } }}
                    pagination={{
                        showSizeChanger: false,
                        pageSize: TableConfigure.pageSize,
                        onChange: e => setParamsData(e),
                    }}
                    dateFormatter="string"
                    scroll={TableConfigure.scroll ? TableConfigure.scroll : { x: 500, y: '53vh' }}
                />
            )}
        </>
    )
}
